home *** CD-ROM | disk | FTP | other *** search
/ Languguage OS 2 / Languguage OS II Version 10-94 (Knowledge Media)(1994).ISO / gnu / glibc108.zip / glibc108 / sunrpc / rpc_cout.c < prev    next >
C/C++ Source or Header  |  1994-02-06  |  7KB  |  351 lines

  1. /* @(#)rpc_cout.c    2.1 88/08/01 4.0 RPCSRC */
  2. /*
  3.  * Sun RPC is a product of Sun Microsystems, Inc. and is provided for
  4.  * unrestricted use provided that this legend is included on all tape
  5.  * media and as a part of the software program in whole or part.  Users
  6.  * may copy or modify Sun RPC without charge, but are not authorized
  7.  * to license or distribute it to anyone else except as part of a product or
  8.  * program developed by the user.
  9.  * 
  10.  * SUN RPC IS PROVIDED AS IS WITH NO WARRANTIES OF ANY KIND INCLUDING THE
  11.  * WARRANTIES OF DESIGN, MERCHANTIBILITY AND FITNESS FOR A PARTICULAR
  12.  * PURPOSE, OR ARISING FROM A COURSE OF DEALING, USAGE OR TRADE PRACTICE.
  13.  * 
  14.  * Sun RPC is provided with no support and without any obligation on the
  15.  * part of Sun Microsystems, Inc. to assist in its use, correction,
  16.  * modification or enhancement.
  17.  * 
  18.  * SUN MICROSYSTEMS, INC. SHALL HAVE NO LIABILITY WITH RESPECT TO THE
  19.  * INFRINGEMENT OF COPYRIGHTS, TRADE SECRETS OR ANY PATENTS BY SUN RPC
  20.  * OR ANY PART THEREOF.
  21.  * 
  22.  * In no event will Sun Microsystems, Inc. be liable for any lost revenue
  23.  * or profits or other special, indirect and consequential damages, even if
  24.  * Sun has been advised of the possibility of such damages.
  25.  * 
  26.  * Sun Microsystems, Inc.
  27.  * 2550 Garcia Avenue
  28.  * Mountain View, California  94043
  29.  */
  30. #ifndef lint
  31. static char sccsid[] = "@(#)rpc_cout.c 1.8 87/06/24 (C) 1987 SMI";
  32. #endif
  33.  
  34. /*
  35.  * rpc_cout.c, XDR routine outputter for the RPC protocol compiler 
  36.  * Copyright (C) 1987, Sun Microsystems, Inc. 
  37.  */
  38. #include <stdio.h>
  39. #include <strings.h>
  40. #include "rpc_util.h"
  41. #include "rpc_parse.h"
  42.  
  43. /*
  44.  * Emit the C-routine for the given definition 
  45.  */
  46. void
  47. emit(def)
  48.     definition *def;
  49. {
  50.     if (def->def_kind == DEF_PROGRAM || def->def_kind == DEF_CONST) {
  51.         return;
  52.     }
  53.     print_header(def);
  54.     switch (def->def_kind) {
  55.     case DEF_UNION:
  56.         emit_union(def);
  57.         break;
  58.     case DEF_ENUM:
  59.         emit_enum(def);
  60.         break;
  61.     case DEF_STRUCT:
  62.         emit_struct(def);
  63.         break;
  64.     case DEF_TYPEDEF:
  65.         emit_typedef(def);
  66.         break;
  67.     }
  68.     print_trailer();
  69. }
  70.  
  71. static
  72. findtype(def, type)
  73.     definition *def;
  74.     char *type;
  75. {
  76.     if (def->def_kind == DEF_PROGRAM || def->def_kind == DEF_CONST) {
  77.         return (0);
  78.     } else {
  79.         return (streq(def->def_name, type));
  80.     }
  81. }
  82.  
  83. static
  84. undefined(type)
  85.     char *type;
  86. {
  87.     definition *def;
  88.  
  89.     def = (definition *) FINDVAL(defined, type, findtype);
  90.     return (def == NULL);
  91. }
  92.  
  93.  
  94. static
  95. print_header(def)
  96.     definition *def;
  97. {
  98.     space();
  99.     f_print(fout, "bool_t\n");
  100.     f_print(fout, "xdr_%s(xdrs, objp)\n", def->def_name);
  101.     f_print(fout, "\tXDR *xdrs;\n");
  102.     f_print(fout, "\t%s ", def->def_name);
  103.     if (def->def_kind != DEF_TYPEDEF ||
  104.         !isvectordef(def->def.ty.old_type, def->def.ty.rel)) {
  105.         f_print(fout, "*");
  106.     }
  107.     f_print(fout, "objp;\n");
  108.     f_print(fout, "{\n");
  109. }
  110.  
  111. static
  112. print_trailer()
  113. {
  114.     f_print(fout, "\treturn (TRUE);\n");
  115.     f_print(fout, "}\n");
  116.     space();
  117. }
  118.  
  119.  
  120. static
  121. print_ifopen(indent, name)
  122.     int indent;
  123.     char *name;
  124. {
  125.     tabify(fout, indent);
  126.     f_print(fout, "if (!xdr_%s(xdrs", name);
  127. }
  128.  
  129.  
  130. static
  131. print_ifarg(arg)
  132.     char *arg;
  133. {
  134.     f_print(fout, ", %s", arg);
  135. }
  136.  
  137.  
  138. static
  139. print_ifsizeof(prefix, type)
  140.     char *prefix;
  141.     char *type;
  142. {
  143.     if (streq(type, "bool")) {
  144.         f_print(fout, ", sizeof(bool_t), xdr_bool");
  145.     } else {
  146.         f_print(fout, ", sizeof(");
  147.         if (undefined(type) && prefix) {
  148.             f_print(fout, "%s ", prefix);
  149.         }
  150.         f_print(fout, "%s), xdr_%s", type, type);
  151.     }
  152. }
  153.  
  154. static
  155. print_ifclose(indent)
  156.     int indent;
  157. {
  158.     f_print(fout, ")) {\n");
  159.     tabify(fout, indent);
  160.     f_print(fout, "\treturn (FALSE);\n");
  161.     tabify(fout, indent);
  162.     f_print(fout, "}\n");
  163. }
  164.  
  165. static
  166. space()
  167. {
  168.     f_print(fout, "\n\n");
  169. }
  170.  
  171. static
  172. print_ifstat(indent, prefix, type, rel, amax, objname, name)
  173.     int indent;
  174.     char *prefix;
  175.     char *type;
  176.     relation rel;
  177.     char *amax;
  178.     char *objname;
  179.     char *name;
  180. {
  181.     char *alt = NULL;
  182.  
  183.     switch (rel) {
  184.     case REL_POINTER:
  185.         print_ifopen(indent, "pointer");
  186.         print_ifarg("(char **)");
  187.         f_print(fout, "%s", objname);
  188.         print_ifsizeof(prefix, type);
  189.         break;
  190.     case REL_VECTOR:
  191.         if (streq(type, "string")) {
  192.             alt = "string";
  193.         } else if (streq(type, "opaque")) {
  194.             alt = "opaque";
  195.         }
  196.         if (alt) {
  197.             print_ifopen(indent, alt);
  198.             print_ifarg(objname);
  199.         } else {
  200.             print_ifopen(indent, "vector");
  201.             print_ifarg("(char *)");
  202.             f_print(fout, "%s", objname);
  203.         }
  204.         print_ifarg(amax);
  205.         if (!alt) {
  206.             print_ifsizeof(prefix, type);
  207.         }
  208.         break;
  209.     case REL_ARRAY:
  210.         if (streq(type, "string")) {
  211.             alt = "string";
  212.         } else if (streq(type, "opaque")) {
  213.             alt = "bytes";
  214.         }
  215.         if (streq(type, "string")) {
  216.             print_ifopen(indent, alt);
  217.             print_ifarg(objname);
  218.         } else {
  219.             if (alt) {
  220.                 print_ifopen(indent, alt);
  221.             } else {
  222.                 print_ifopen(indent, "array");
  223.             }
  224.             print_ifarg("(char **)");
  225.             if (*objname == '&') {
  226.                 f_print(fout, "%s.%s_val, (u_int *)%s.%s_len",
  227.                     objname, name, objname, name);
  228.             } else {
  229.                 f_print(fout, "&%s->%s_val, (u_int *)&%s->%s_len",
  230.                     objname, name, objname, name);
  231.             }
  232.         }
  233.         print_ifarg(amax);
  234.         if (!alt) {
  235.             print_ifsizeof(prefix, type);
  236.         }
  237.         break;
  238.     case REL_ALIAS:
  239.         print_ifopen(indent, type);
  240.         print_ifarg(objname);
  241.         break;
  242.     }
  243.     print_ifclose(indent);
  244. }
  245.  
  246.  
  247. /* ARGSUSED */
  248. static
  249. emit_enum(def)
  250.     definition *def;
  251. {
  252.     print_ifopen(1, "enum");
  253.     print_ifarg("(enum_t *)objp");
  254.     print_ifclose(1);
  255. }
  256.  
  257.  
  258. static
  259. emit_union(def)
  260.     definition *def;
  261. {
  262.     declaration *dflt;
  263.     case_list *cl;
  264.     declaration *cs;
  265.     char *object;
  266.     char *format = "&objp->%s_u.%s";
  267.  
  268.     print_stat(&def->def.un.enum_decl);
  269.     f_print(fout, "\tswitch (objp->%s) {\n", def->def.un.enum_decl.name);
  270.     for (cl = def->def.un.cases; cl != NULL; cl = cl->next) {
  271.         cs = &cl->case_decl;
  272.         f_print(fout, "\tcase %s:\n", cl->case_name);
  273.         if (!streq(cs->type, "void")) {
  274.             object = alloc(strlen(def->def_name) + strlen(format) +
  275.                        strlen(cs->name) + 1);
  276.             s_print(object, format, def->def_name, cs->name);
  277.             print_ifstat(2, cs->prefix, cs->type, cs->rel, cs->array_max,
  278.                      object, cs->name);
  279.             free(object);
  280.         }
  281.         f_print(fout, "\t\tbreak;\n");
  282.     }
  283.     dflt = def->def.un.default_decl;
  284.     if (dflt != NULL) {
  285.         if (!streq(dflt->type, "void")) {
  286.             f_print(fout, "\tdefault:\n");
  287.             object = alloc(strlen(def->def_name) + strlen(format) +
  288.                        strlen(dflt->name) + 1);
  289.             s_print(object, format, def->def_name, dflt->name);
  290.             print_ifstat(2, dflt->prefix, dflt->type, dflt->rel,
  291.                      dflt->array_max, object, dflt->name);
  292.             free(object);
  293.             f_print(fout, "\t\tbreak;\n");
  294.         }
  295.     } else {
  296.         f_print(fout, "\tdefault:\n");
  297.         f_print(fout, "\t\treturn (FALSE);\n");
  298.     }
  299.     f_print(fout, "\t}\n");
  300. }
  301.  
  302.  
  303.  
  304. static
  305. emit_struct(def)
  306.     definition *def;
  307. {
  308.     decl_list *dl;
  309.  
  310.     for (dl = def->def.st.decls; dl != NULL; dl = dl->next) {
  311.         print_stat(&dl->decl);
  312.     }
  313. }
  314.  
  315.  
  316.  
  317.  
  318. static
  319. emit_typedef(def)
  320.     definition *def;
  321. {
  322.     char *prefix = def->def.ty.old_prefix;
  323.     char *type = def->def.ty.old_type;
  324.     char *amax = def->def.ty.array_max;
  325.     relation rel = def->def.ty.rel;
  326.  
  327.     print_ifstat(1, prefix, type, rel, amax, "objp", def->def_name);
  328. }
  329.  
  330.  
  331.  
  332.  
  333.  
  334. static
  335. print_stat(dec)
  336.     declaration *dec;
  337. {
  338.     char *prefix = dec->prefix;
  339.     char *type = dec->type;
  340.     char *amax = dec->array_max;
  341.     relation rel = dec->rel;
  342.     char name[256];
  343.  
  344.     if (isvectordef(type, rel)) {
  345.         s_print(name, "objp->%s", dec->name);
  346.     } else {
  347.         s_print(name, "&objp->%s", dec->name);
  348.     }
  349.     print_ifstat(1, prefix, type, rel, amax, name, dec->name);
  350. }
  351.